/**
 * UserInfoSingleton.java
 * 版权所有(C) 2013
 * 创建者:cuiran 2013-12-16 下午12:19:27
 */
package com.ghyf.mplay.application;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import android.text.TextUtils;




/**
 * 存储系统设置 SharedPreferences 
 * @author cuiran
 * @version 1.0.0
 */
public class UserInfoSingleton {
	/**
	 * 是否直播 0 紧急 1 正常
	 */
	private static String isExigent;
	private static String playPath;

	public static int timesamp;
	
	public static boolean isUpdate=true;
	
	/**
	 * 是否直播 0 本地 1 直播
	 */
	private static String islive;
	/**
	 * 是否重启 0 默认 1 重启,目前无用
	 */
	private static String isreboot;
	
	/**
	 * @return the isreboot
	 */
	public static String getIsreboot() {
		isreboot=BaseCookie.sysShare.getString(CookieConstants.ISREBOOT,"");
		return isreboot;
	}

	/**
	 * @param isreboot the isreboot to set
	 */
	public static void setIsreboot(String isreboot) {
		UserInfoSingleton.isreboot = isreboot;
		BaseCookie.sysEdit.putString(CookieConstants.ISREBOOT, isreboot);
		BaseCookie.sysEdit.commit();
	}

	public static boolean putStringAndCommit(String key, String value) {
		return BaseCookie.sysEdit.putString(key, value).commit();
	}

	public static boolean putIntAndCommit(String key, int value) {
		return BaseCookie.sysEdit.putInt(key, value).commit();
	}

	public static boolean putBooleanAndCommit(String key, boolean value) {
		return BaseCookie.sysEdit.putBoolean(key, value).commit();
	}
	
	public static String getString(String key) {
		return getString(key, "");
	}

	public static String getString(String key, String defValue) {
		return BaseCookie.sysShare.getString(key, defValue);
	}

	public static int getInt(String key) {
		return getInt(key, -1);
	}

	public static int getInt(String key, int defValue) {
		return BaseCookie.sysShare.getInt(key, defValue);
	}

	public static boolean getBoolean(String key) {
		return getBoolean(key, false);
	}

	public static boolean getBoolean(String key, boolean defValue) {
		return BaseCookie.sysShare.getBoolean(key, defValue);
	}
	/**
	 * @return the isExigent
	 */
	public static String getIsExigent() {
		isExigent=BaseCookie.sysShare.getString(CookieConstants.ISEXIGENT,"");
		return isExigent;
	}

	/**
	 * @param isExigent the isExigent to set
	 */
	public static void setIsExigent(String isExigent) {
		UserInfoSingleton.isExigent = isExigent;
		BaseCookie.sysEdit.putString(CookieConstants.ISEXIGENT, isExigent);
		BaseCookie.sysEdit.commit();
	}

	/**
	 * @return the islive
	 */
	public static String getIslive() {
		islive=BaseCookie.sysShare.getString(CookieConstants.ISLIVE,"");
		return islive;
	}

	/**
	 * @param islive the islive to set
	 */
	public static void setIslive(String islive) {
		UserInfoSingleton.islive = islive;
		BaseCookie.sysEdit.putString(CookieConstants.ISLIVE, islive);
		BaseCookie.sysEdit.commit();
	}

	/**
	 * @return the playPath
	 */
	public static String getPlayPath() {
		playPath=BaseCookie.sysShare.getString(CookieConstants.PLAYPATH,"");
		return playPath;
	}

	/**
	 * @param playPath the playPath to set
	 */
	public static void setPlayPath(String playPath) {
		UserInfoSingleton.playPath = playPath;
		BaseCookie.sysEdit.putString(CookieConstants.PLAYPATH, playPath);
		BaseCookie.sysEdit.commit();
	}
	
	  /**
	   * desc:保存对象
	   * @param context
	   * @param key 
	   * @param obj 要保存的对象，只能保存实现了serializable的对象
	   * modified:    
	   */
	  public static void saveObject(String key ,Object obj){
	      try {
	          // 保存对象
	          //先将序列化结果写到byte缓存中，其实就分配一个内存空间
	          ByteArrayOutputStream bos=new ByteArrayOutputStream();
	          ObjectOutputStream os=new ObjectOutputStream(bos);
	          //将对象序列化写入byte缓存
	          os.writeObject(obj);
	          //将序列化的数据转为16进制保存
	          String bytesToHexString = bytesToHexString(bos.toByteArray());
	          //保存该16进制数组
	          BaseCookie.sysEdit.putString(key, bytesToHexString);
	          BaseCookie.sysEdit.commit();
	      } catch (IOException e) {
	          e.printStackTrace();
	      }
	  }
	
	  /**
	   * desc:将数组转为16进制
	   * @param bArray
	   * @return
	   * modified:    
	   */
	  public static String bytesToHexString(byte[] bArray) {
	      if(bArray == null){
	          return null;
	      }
	      if(bArray.length == 0){
	          return "";
	      }
	      StringBuffer sb = new StringBuffer(bArray.length);
	      String sTemp;
	      for (int i = 0; i < bArray.length; i++) {
	          sTemp = Integer.toHexString(0xFF & bArray[i]);
	          if (sTemp.length() < 2)
	              sb.append(0);
	          sb.append(sTemp.toUpperCase());
	      }
	      return sb.toString();
	  }
	  /**
	   * desc:获取保存的Object对象
	   * @param context
	   * @param key
	   * @return
	   * modified:    
	   */
	  public static Object readObject(String key){
	      try {
	          if (BaseCookie.sysShare.contains(key)) {
	        	  String string = BaseCookie.sysShare.getString(key, "");
	        	  if(TextUtils.isEmpty(string)){
	                   return null;
	              
	        	  }else{
	                   //将16进制的数据转为数组，准备反序列化
	                   byte[] stringToBytes = StringToBytes(string);
	                     ByteArrayInputStream bis=new ByteArrayInputStream(stringToBytes);
	                     ObjectInputStream is=new ObjectInputStream(bis);
	                     //返回反序列化得到的对象
	                     Object readObject = is.readObject();
	                     return readObject;
	               }
	          }
	      } catch (StreamCorruptedException e) {
	          // TODO Auto-generated catch block
	          e.printStackTrace();
	      } catch (IOException e) {
	          // TODO Auto-generated catch block
	          e.printStackTrace();
	      } catch (ClassNotFoundException e) {
	          // TODO Auto-generated catch block
	          e.printStackTrace();
	      }
	      //所有异常返回null
	      return null;
	       
	  }
	  
	  /**
	   * desc:将16进制的数据转为数组
	   * @param data
	   * @return
	   * modified:    
	   */
	  public static byte[] StringToBytes(String data){
	        String hexString=data.toUpperCase().trim();
	        if (hexString.length()%2!=0) {
	            return null;
	        }
	        byte[] retData=new byte[hexString.length()/2];
	        for(int i=0;i<hexString.length();i++)
	        {
	        	  int int_ch;  // 两位16进制数转化后的10进制数
		            char hex_char1 = hexString.charAt(i); ////两位16进制数中的第一位(高位*16)
		            int int_ch1;
		            if(hex_char1 >= '0'&& hex_char1 <='9')
		                int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
		            else if(hex_char1 >= 'A'&&hex_char1 <='F')
		                int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
		            else
		                return null;
	            i++;
	            char hex_char2 = hexString.charAt(i); ///两位16进制数中的第二位(低位)
	            int int_ch2;
	            if(hex_char2 >= '0' && hex_char2 <='9')
	                int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
	            else if(hex_char2 >= 'A'&& hex_char2 <='F')
	                int_ch2 = hex_char2-55; //// A 的Ascll - 65
	            else
	                return null;
	            int_ch = int_ch1+int_ch2;
	            retData[i/2]=(byte) int_ch;//将转化后的数放入Byte里
	        }
	        return retData;
	  }
}
